package lichbaogiang;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;


public class MonHoc extends JFrame{

    //-------------
    String st1 = "";
    String st2 = "";
    String st3 = "";
    String st4 = "";
    String st5 = "";
    String st6 = "";
    String st7 = "";
    String st8 = "";
    String st9 = "";
    
    static int checkInsert = 0;
    
    JTable table = new JTable();
    DefaultTableModel tablemodel = new DefaultTableModel();
    String data1[] = new String[20];
    JPanel pn = new JPanel(new BorderLayout());
    JFrame f = new JFrame();
    JToolBar toolBar;
    InsertSubject insert;
    JLabel lbMH;
    
    //--------DataBase Connection--------//
    Connection conn;
    Statement stmt;
    ResultSet rs;
    ResultSetMetaData rsmd;
    String url;
    static int count_row = 0;
    
    public void gui()
    {
        f.setVisible(true);
        f.setSize(800, 600);
        
        toolBar = new JToolBar();
        toolBar.setEnabled(false);
        toolBar.setBackground(Color.decode("#FFE4B5"));
        toolBar.addSeparator(new Dimension(70,0));
        
        //add but into toolbar
        //--------Insert--------//
        ImageIcon icon = new ImageIcon(getClass().getResource("icon/Insert.gif"));
	JButton but = new JButton(icon); 
        but.setText("Chèn");
        but.setBackground(Color.decode("#00FFFF"));
	but.setToolTipText("Chèn");  
	but.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                InsertInformationSubject(e);
            }
        }); 
	toolBar.add(but); 
        toolBar.addSeparator(new Dimension(100,50));
        
        //---------Delete----------//
        icon = new ImageIcon(getClass().getResource("icon/Delete.gif"));
	but = new JButton(icon); 
        but.setText("Xóa");
        but.setBackground(Color.decode("#00FFFF"));
	but.setToolTipText("Xóa");  
	but.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DeleteInformationSubject(e);
            }
        }); 
	toolBar.add(but); 
        toolBar.addSeparator(new Dimension(100,50));
        
        //-------Update-------//
        icon = new ImageIcon(getClass().getResource("icon/Update.gif"));
	but = new JButton(icon); 
        but.setText("Cập Nhật");
        but.setBackground(Color.decode("#00FFFF"));
	but.setToolTipText("Cập Nhật");  
	but.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                UpdateInformationSubject(e);
            }
        }); 
	toolBar.add(but);
        toolBar.addSeparator(new Dimension(100,50));
        
        //--------Exit--------//
        icon = new ImageIcon(getClass().getResource("icon/Exit.gif"));
	but = new JButton(icon); 
        but.setText("Thoát");
        but.setBackground(Color.decode("#00FFFF"));
	but.setToolTipText("Thoát");  
	but.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(JOptionPane.showConfirmDialog(null,"Bạn muốn kết thúc?","",JOptionPane.YES_NO_OPTION) == 0) 
                {
                    f.setVisible(false);
                }
            }
        }); 
	toolBar.add(but);
        
        //table.setFont(new Font("Calibri", 0, 18));
        pn.setBackground(Color.green);
        lbMH = new JLabel("DANH SÁCH LỚP HỌC PHẦN");
        pn.add(lbMH, BorderLayout.NORTH);
        
        pn.add(new JScrollPane(table), BorderLayout.CENTER);
        f.add(pn, BorderLayout.CENTER);
        f.add(toolBar, BorderLayout.NORTH);
        f.setTitle("LỚP HỌC PHẦN");
    }
    
    public void loadingdata(String sql)
    {
            String colum[] = {"STT","TenHP","TC","LT","TH","DA","TT","LOP","MaLopHP"}; 
            //columNames = colum;
            tablemodel.setColumnIdentifiers(colum);
            table.setModel(tablemodel);
            url = "jdbc:odbc:CNPM";
            //String sql = "select * from DSPC";
            String data1[] = new String[20];
            try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection(url);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            rsmd = rs.getMetaData();
            count_row = 0;
            while(rs.next())
            { 
                data1[0]=String.valueOf(rs.getInt(1));
                data1[1]=rs.getString(2);
                data1[2]=String.valueOf(rs.getInt(3));
                data1[3]=String.valueOf(rs.getInt(4));
                data1[4]= String.valueOf(rs.getInt(5));   
                data1[5]= String.valueOf(rs.getInt(6));
                data1[6]= String.valueOf(rs.getInt(7));
                data1[7]= rs.getString(8);
                data1[8]= rs.getString(9);
                tablemodel.addRow(data1);
                count_row ++;
            }
        } catch(Exception ex){  
          System.out.println("Error " + ex); }
        }
    public MonHoc()
    {
        gui();
        String sql = "select * from DSPC";
        loadingdata(sql);
    }

    /*public static void main(String[] args)
    {
        new MonHoc();

    }*/

    public void InsertInformationSubject(ActionEvent e)
    {
        checkInsert = 1 - checkInsert;
        if(checkInsert != 0){
            lbMH.setText("");
            insert = new InsertSubject();
            pn.add(insert, BorderLayout.NORTH);
            f.setVisible(true);
        }
        else{
            lbMH.setText("DANH SÁCH LỚP HỌC PHẦN");
            insert.hide();
            pn.add(lbMH, BorderLayout.NORTH);
        }
    }
    
    public void DeleteInformationSubject(ActionEvent e)
    {
        //insert.hide();
        int  row = table.getSelectedRow();
        //System.out.println(row);
        String st = (String) table.getValueAt(table.getSelectedRow(), 8);
        String sql = "delete from DSPC where MLHP = '"+st+"'";
        if(JOptionPane.showConfirmDialog(null,"Bạn muốn xóa không?","Yes/No!",JOptionPane.YES_NO_OPTION) == 0) 
        {
            tablemodel.removeRow(row);
            loadingdata(sql);
            count_row --;
        }
    }
    
    public void UpdateInformationSubject(ActionEvent e)
    {
        checkInsert = 1 - checkInsert;
         /*if(JOptionPane.showConfirmDialog(null,"Bạn muốn lưu lại không?","Thông báo",JOptionPane.YES_NO_OPTION)==0) 
        {
            
            String sql="";
            int kt=tablemodel.getRowCount();
            for(int j=0;j<kt;j++)
            {
                st1=(String)tablemodel.getValueAt(j, 0);
                st2=(String)tablemodel.getValueAt(j, 1);
                st3=(String)tablemodel.getValueAt(j, 2);
                st4=(String)tablemodel.getValueAt(j, 3);
                st5=(String)tablemodel.getValueAt(j, 4);
                st6=(String)tablemodel.getValueAt(j, 5);
                st7=(String)tablemodel.getValueAt(j, 6);
                st8=(String)tablemodel.getValueAt(j, 7);
                st9=(String)tablemodel.getValueAt(j, 8);
               
                sql="update DSPC set TenHP='"+st2+"' TC='"
                        +st3+"' TH='"+st4+"' LT='"
                        +st5+"' DA='"+st6+"' TT='"
                        +st7+"' Lop='"+st8+"' MLHP='"+st9+"' where MSGV='"+st1+"'";
                        try {
                        Connection con = DriverManager.getConnection(url);
                        Statement stm = con.createStatement();
                        stm.executeUpdate(sql);
                    } catch (SQLException ex) {
                    Logger.getLogger(Lichbaogiang.class.getName()).log(Level.SEVERE, null, ex);
            }  
        }
            
        }*/
         f.setVisible(false);
            new MonHoc();
    }
     private void TableKeyPressed(java.awt.event.KeyEvent evt) {                                  
        int dong=table.getSelectedRow();
        int cot=table.getSelectedColumn();
        TableCellEditor tam= table.getCellEditor(cot,dong);
        table.setValueAt(tam, cot, dong);
    }
    }
    
